package t;

import org.junit.Test;

import java.lang.reflect.Field;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

public class Network {
    /**
     * TODO 根据ip地址和子网掩码求网段
     * 网段 = ip地址 & 子网掩码
     */
    @Test
    public void t1() {
        //ip 1100 0000.1010 1000.0000 0001.0000 1010 = 192.168.1.10
        String ip01 = "11000000";
        String ip02 = "10101000";
        String ip03 = "00000001";
        String ip04 = "00001010";
        //子网掩码 1111 1111.1111 1111.1111 1111.0000 0000 = 255.255.255.0
        String n1 = "11111111";
        String n2 = "11111111";
        String n3 = "11111111";
        String n4 = "000000";
        System.out.print((Integer.parseInt(ip01, 2) & Integer.parseInt(n1, 2)) + "."
                + (Integer.parseInt(ip02, 2) & Integer.parseInt(n2, 2)) + "."
                + (Integer.parseInt(ip03, 2) & Integer.parseInt(n3, 2)) + "."
                + (Integer.parseInt(ip04, 2) & Integer.parseInt(n4, 2)) + ".");
    }

    /**
     * TODO 将十进制IP转为二进制
     * 根据IP地址判断属于A/B/C类地址
     * 将十进制ip地址转为二进制
     * A类地址以0开头-对应的子网掩码为：255.0.0.0
     * B类地址以10开头-对应的子网掩码为：255.255.0.0
     * C类地址以110开头-对应的子网掩码为：255.255.255.0
     */
    @Test
    public void t2() {
        int ip1 = 192;
        int ip2 = 168;
        int ip3 = 1;
        int ip4 = 1;
        System.out.println(Integer.toBinaryString(ip1) + '.'
                + Integer.toBinaryString(ip2) + '.'
                + Integer.toBinaryString(ip3) + '.'
                + Integer.toBinaryString(ip4));
    }

    /**
     * TODO 二进制IP转为十进制
     */
    @Test
    public void t3() {
        String ip1 = "11000000";
        String ip2 = "10101000";
        String ip3 = "00000001";
        String ip4 = "00001010";
        System.out.print(Integer.parseInt(ip1, 2) + "."
                + Integer.parseInt(ip2, 2) + "."
                + Integer.parseInt(ip3, 2) + "."
                + Integer.parseInt(ip4, 2));
    }
    @Test
    public void tes() throws NoSuchFileException, IllegalAccessException {
        ArrayList<Integer> list1 = new ArrayList<>();
        CopyOnWriteArrayList<Integer> list2 = new CopyOnWriteArrayList<>();
        list1.add(1);
        list2.add(1);
        Class c1 = ArrayList.class;
        Class c2 = CopyOnWriteArrayList.class;
        

        String str = "alpha. beta. gamma.";
        String regex = "\\.\\s*";
        String[] res = str.split(regex);
        for (String s : res){
            System.out.print(s);
        }
    }


}
